package com.every.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.every.entity.EmailRecord;
import com.every.mapper.EmailRecordMapper;
import com.every.service.EmailRecordService;
import org.springframework.stereotype.Service;

/**
 * @description: 邮件发送记录
 * @author: yh
 * @date: 2022/11/1
 */
@Service
public class EmailRecordImpl extends ServiceImpl<EmailRecordMapper, EmailRecord> implements EmailRecordService {
    /**
     * 添加发送记录
     * @param content 发送内容
     * @param userId 发送用户id
     * @author yh
     * @date 2022/11/1
     * @return 发送记录id
     */
    @Override
    public Long saveEmailRecord(String content, Long userId){
        EmailRecord obj = new EmailRecord();
        obj.setContent(content);
        obj.setUserId(userId);
        baseMapper.insert(obj);
        return obj.getRecordId();
    }

    /**
     * 更新邮件发送状态
     * @param recordId 记录id
     * @author yh
     * @date 2022/11/1
     */
    @Override
    public void updateState(Long recordId){
        LambdaUpdateChainWrapper<EmailRecord> wrapper = new LambdaUpdateChainWrapper<>(baseMapper);
        wrapper.eq(EmailRecord::getRecordId, recordId)
                .set(EmailRecord::getState, true)
                .update();
    }

    /**
     * 根据用户获取发送次数
     * @param  userId 用户id
     * @author yh
     * @date 2022/11/1
     * @return 发送了多少次
     */
    @Override
    public Long getSendCount(Long userId){
        LambdaQueryWrapper<EmailRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmailRecord::getUserId, userId)
                .eq(EmailRecord::getState, true);
        return baseMapper.selectCount(wrapper);
    }
}
